package com.ruoyi.web.controller.api;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.estate.UserCollection;
import com.ruoyi.system.service.estate.ICollectionService;

/**
 * 收藏API Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/api/collection")
public class CollectionApiController extends BaseController {
    
    @Autowired
    private ICollectionService collectionService;
    
    /**
     * 获取收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserCollection collection) {
        startPage();
        // 设置当前用户ID
        SysUser user = SecurityUtils.getLoginUser().getUser();
        collection.setUserId(user.getUserId());
        // 设置查询条件：状态正常
        collection.setStatus("0");
        collection.setDelFlag("0");
        List<UserCollection> list = collectionService.selectCollectionList(collection);
        return getDataTable(list);
    }
    
    /**
     * 获取公告收藏列表
     */
    @GetMapping("/notice/list")
    public TableDataInfo noticeList() {
        startPage();
        try {
            // 获取当前用户ID
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long userId = user.getUserId();
            
            // 查询用户收藏的公告列表
            List<UserCollection> list = collectionService.selectNoticeCollectionList(userId);
            return getDataTable(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new TableDataInfo();
        }
    }
    
    /**
     * 获取新闻收藏列表
     */
    @GetMapping("/news/list")
    public TableDataInfo newsList() {
        startPage();
        try {
            // 获取当前用户ID
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long userId = user.getUserId();
            
            // 查询用户收藏的新闻列表
            List<UserCollection> list = collectionService.selectNewsCollectionList(userId);
            return getDataTable(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new TableDataInfo();
        }
    }
    
    /**
     * 添加收藏
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody UserCollection collection) {
        // 设置当前用户ID
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        
        // 检查参数
        if (collection.getTargetId() == null || collection.getCollectionType() == null) {
            return AjaxResult.error("参数错误");
        }
        
        // 添加收藏
        int rows = collectionService.addCollection(userId, collection.getCollectionType(), collection.getTargetId());
        return toAjax(rows);
    }
    
    /**
     * 取消收藏
     */
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody UserCollection collection) {
        // 设置当前用户ID
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        
        // 检查参数
        if (collection.getTargetId() == null || collection.getCollectionType() == null) {
            return AjaxResult.error("参数错误");
        }
        
        // 取消收藏
        int rows = collectionService.cancelCollection(userId, collection.getCollectionType(), collection.getTargetId());
        return toAjax(rows);
    }
    
    /**
     * 检查是否已收藏
     */
    @GetMapping("/check")
    public AjaxResult check(UserCollection collection) {
        // 设置当前用户ID
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        
        // 检查参数
        if (collection.getTargetId() == null || collection.getCollectionType() == null) {
            return AjaxResult.error("参数错误");
        }
        
        // 检查是否已收藏（包括已删除的记录）
        UserCollection existingCollection = collectionService.checkCollectionIncludeDeleted(userId, collection.getCollectionType(), collection.getTargetId());
        boolean isCollected = (existingCollection != null && "0".equals(existingCollection.getDelFlag()));
        
        return AjaxResult.success(isCollected);
    }
} 